bookname = ['雪落香杉树','人间失格','活着',"浮生六记"]
books = {1:['雪落香杉树','伽特森', 5, 0],2:['人间失格', '太宰治', 5, 10],3:['活着', '余华', 0, 10],4:["浮生六记", '沈复', 0, 10]}

register_students = []
import time
from two_test import fontcolor_change as font

class Bookmanage():
    whether_return1 = 0

    def __init__(self,admin = '姐姐'):
        self.admin = admin

    def show_book(self):
        print("书籍信息显示".center(50, '*'))
        print('''
        编号      书名      作者    borrowed|   库存
                    ''')
        for i in books:
            print('{}       {} '.format(i, books[i]))    #  书籍信息显示

    def find(self,name):
        """
        根据用户传入的书籍名称判断书籍是否存在,
        如果存在, 返回该书籍对象;
        如果不存在, 返回None
        """
        if name in bookname:
             return name
        else:
            return  font.Error + "书籍%s不存在" %(name) + font.END

    def recordlenddate(self):    #  何时借出书籍
            stime1 = time.localtime()
            stime2 = time.strftime('%Y -%m -%d', stime1)
            # print(time.strftime('%Y', stime1))
            # print(time.strftime('%m', stime1))
            # print(time.strftime('%d', stime1))
            return '{}:借出书籍'.format(stime2)

    def chat_explain(self):     #  管理员对您借的书的说明，看法
        lendnumber = input('看了一圈的您请问要借几本书 ?')
        lendnumber = int(lendnumber)
        while lendnumber > 0:
            code = input('请说出编号:')
            code = int(code)
            name = input('请说出您将要借的书籍:')
            book = self.find(name)
            if book:
                if books[code][3] > 0:
                    if name in books[code]:
                        dict_information = dict(zip(bookname, font.bookinformation))
                        print('小弟弟，{}跟你说哟:  这书{}'.format(self.admin,dict_information[name]))
                        # self.recordlenddate()
                        books[code][2] += 1
                        books[code][3] -= 1
                        print(font.Ok + "书籍借阅成功" + font.END)
                    else:
                        print(font.warning + "书籍名与编码不符"  + font.END)
                        lendnumber += 1
                else:
                    print(font.warning + "书籍%s库存不足，已被抢借一空" % (name) + font.END)
                    lendnumber += 1
            else:
                print(font.Error + "书籍%s不存在" % (name) + font.END)
            lendnumber = int(lendnumber)
            lendnumber -= 1

    def return_book(self): # 还书   将 还书状况 append到  类属性 ：[] whether_return
        whether_return = []
        print(books)
        lendnumber = input('您借了几本书？：')
        lendnumber = int(lendnumber)
        returnnumber = input('您要还几本书：')
        returnnumber = int(returnnumber)
        while returnnumber > 0:
            name = input("归还书籍名称：").strip()
            code = input('书籍的编号')
            code = int(code)
            book = self.find(name)
            if book:
                if name in books[code]:
                    if lendnumber == returnnumber:
                        whether_return.append(' v ，都已归还!')
                        Bookmanage.whether_return1 = whether_return
                        books[code][2] -= 1
                        books[code][3] += 1
                        print(font.Ok + "书籍归还成功" + font.END)
                        break
                    else:
                        books[code][2] -= 1
                        books[code][3] += 1
                        print(font.Ok + "书籍归还成功" + font.END)
                        whether_return.append(' ×，{}本已归还!:{}'.format(returnnumber, name))
                        Bookmanage.whether_return1 = whether_return
                else:
                    print(font.warning + "书籍名与编码不匹配" + font.END)
                    lendnumber += 1
            else:
                print(font.Error + "书籍%s不存在" % (name) + font.END)
            returnnumber = int(returnnumber)
            returnnumber -= 1

    def lendbook_name(self):      #  借的书籍的详细信息
        lendinformation = []
        lendnumber = input('抱歉要登记重复的再问您一下要借几本书 ?')
        name = input('请把你将要借的书籍的详细信息发一下:')
        lendinformation.append(lendnumber)
        lendinformation.append(name)
        return lendinformation

    def isleapyear(self,year):   # 判断闰年平年
        return year%400==0 or (year % 100!=0 and year%4 ==0)

    def havedays(self,year,month):  #判断月份的天数
        list31 = [1, 3, 5, 7, 8, 10, 12]
        list30 = [4, 6, 9, 11]
        if month in list31:
            return 31
        elif month in list30:
            return 30
        elif month == 2:
            if self.isleapyear(year):
                return 29
            else:
                return 28
        else:
            return 0

    def recordreturndate(self):    #    计算还书日期
        day = input('最多借30天您借几天?')
        day = int(day)
        stime1 = time.localtime()
        stime_year = time.strftime('%Y',stime1)
        stime_year = int(stime_year)
        stime_month = time.strftime('%m',stime1)
        stime_month = int(stime_month)
        stime_day = time.strftime('%d',stime1)
        stime_day = int(stime_day)
        res = self.havedays(stime_year, stime_month)    # 本月的天数
        day1 = stime_day+day    #借书的天数+现在的天数=day1
        if day1 < res:  #   if  day1<本月的天数  就本月： day1归还
            return  '应{}-{}返回书籍'.format(stime_month,day1)
        else: # if  day1》本月的天数  就下月： day1-本月的天数 - 归还
            stime_month = stime_month+1
            stime_day1 = day1-res
            return '应{}-{}返回书籍'.format(stime_month,stime_day1)

    @staticmethod
    def show_bookmanage():
        print('This is Book馆 Administrator类')





class Students():

    def __init__(self, sid,name, age, lendbookname, lenddate, returndate,isreturn):
        self.sid= sid
        self.name = name
        self.age= age
        self.lendbookname = lendbookname
        self.lenddate = lenddate
        self.returndate = returndate
        self.isreturn = isreturn

    @classmethod
    def lendbook_select(cls):
        print("""
        书籍借阅与管理系统
        1).查询书籍信息
        2).借书
        3).查询学生借书的情况
        4).还书
        5).退出系统
        请输入你的选择：""")



def main():
    id_list = []
    global register_students
    bm = Bookmanage()
    print(font.Ok + "初始化书籍信息成功" + font.END)
    Students.lendbook_select()
    while 1:
        a = input('请输入指令：')
        if a == '1':
            bm.show_book()

        elif a == '2':
            bm.chat_explain()
            lenddate = bm.recordlenddate()
            lendname = bm.lendbook_name()
            returndate = bm.recordreturndate()
            print('请将书交给管理员并进行信息登记')
            register_sid = input('请输入学号:')
            register_name = input('请输入姓名:')
            register_age = input('请输入年龄:')
            s1 = Students(register_sid,register_name,register_age,lendname,lenddate,returndate,0)
            register_students.append(s1.__dict__)
            id_list.append(register_sid)
            print(font.Ok + "登记完成，欢迎下次光临！" + font.END)

        elif a == '3':
            print('借书学生信息情况如下：')
            for i in register_students:
                print(i)

        elif a == '4':
            sid = input('输入借书人id：')
            bm.return_book()
            register_students[id_list.index(sid)]['isreturn'] = bm.whether_return1

        elif a == '5':
            print('已成功退出！')
            break

main()







